package ui;

import com.sun.xml.internal.ws.api.streaming.XMLStreamReaderFactory;
import log.LogFileStream;
import log.LogItem;
import log.LogParserIOS;
import sun.misc.JavaLangAccess;

import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.TableColumnModelListener;
import javax.swing.table.TableColumn;
import javax.swing.table.TableColumnModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.lang.ref.WeakReference;

public class MainFrame
{
    private static final int MinWidth = 1200;
    private static final int MinHeight = 800;
    private static final int DefaultWidth = 1200;
    private static final int DefaultHeight = 800;


    public void create()
    {
        _frame = new JFrame("XLogView");
        _frame.setLayout(new BorderLayout());
        _frame.setSize(DefaultWidth,DefaultHeight);
        _frame.setMinimumSize(new Dimension(MinWidth, MinHeight));
        _frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

        createToolbar();
        createLogPanel();
        createFindBar();
        createStatusBar();

        // 这一行放这里是因为放前面，后面添加的控件不会显示出来
        _frame.setVisible(true);
    }

    private void createToolbar()
    {
        _mainToolbar = new JToolBar();
        _mainToolbar.setFloatable(false);

        JButton btnOpen = new JButton();
        _mainToolbar.add(btnOpen);
        btnOpen.setText("Open");
        btnOpen.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                MainFrame.this.openFileSelector();
            }
        });

        _frame.add(_mainToolbar, BorderLayout.PAGE_START);
    }

    private void createFindBar() {

        _findBar = new JPanel();
        _findBar.setLayout(new FlowLayout(FlowLayout.LEFT));
        _findBar.setSize(DefaultWidth, 20);
        _findBar.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.LOWERED));

        JTextField text = new JTextField();
        text.setMinimumSize(new Dimension(200, 20));
        text.setMaximumSize(new Dimension(600, 20));
        text.setPreferredSize(new Dimension(400, 20));
        text.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {


            }
        });
        _findBar.add(text);


        JButton nextButton = new JButton("next");
        JButton prevButton = new JButton("prev");

        _findBar.add(prevButton);
        _findBar.add(nextButton);


        _mainPanel.add(_findBar, BorderLayout.NORTH);
    }

    private void openFileSelector() {

        JFileChooser fileChooser = new JFileChooser();
        fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        fileChooser.showDialog(new JLabel(), "select");
        File file = fileChooser.getSelectedFile();
        if (file != null) {
            openLogFile(file);
        }
    }

    private void openLogFile(File file) {
        closeLogFile();

        if (file == null) {
            return;
        }

        LogTableModel model = new LogTableModel();
        model.addLogStream(new LogFileStream(file.getPath(), new LogParserIOS()));
        _logTable.setModel(model);
        setLogTableColumn();
    }

    private void closeLogFile() {

    }

    private void createLogPanel()
    {
        _mainPanel = new JPanel(new BorderLayout(1, 0), true);

        _logTable = new JTable();

        _logPanel = new JScrollPane();
        _logPanel.setBorder(new EmptyBorder(0,0,0,0));
        _logPanel.setViewportView(_logTable);
        _logPanel.setMinimumSize(new Dimension(400, 400));


        _logTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
            @Override
            public void valueChanged(ListSelectionEvent e) {

                int row = _logTable.getSelectedRow();
                if (row < 0) {
                    return;
                }
                String logContent = _logTable.getModel().getValueAt(row, LogItem.Field.Message.ordinal()).toString();
                _logContent.setText(logContent);
            }
        });


        _logContent = new JTextArea();
        _logContent.setMinimumSize(new Dimension(400, 50));
        _logContent.setLineWrap(true);
        _logContent.setTabSize(4);
        //_logContent.setEditable(false);

        _logSplitPanel = new JSplitPane(JSplitPane.VERTICAL_SPLIT, _logPanel, _logContent);
        _logSplitPanel.setDividerSize(3);
        _logSplitPanel.setDividerLocation(0.9);
        _logSplitPanel.setResizeWeight(1.0);
        _mainPanel.add(_logSplitPanel, BorderLayout.CENTER);

        _frame.add(_mainPanel, BorderLayout.CENTER);
    }

    private void setLogTableColumn() {

        //_logTable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
        //_logTable.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN);

        TableColumnModel cm = _logTable.getColumnModel();
        cm.getColumn(LogItem.Field.Index.ordinal()).setWidth(30);
        cm.getColumn(LogItem.Field.Index.ordinal()).setPreferredWidth(30);
        cm.getColumn(LogItem.Field.Index.ordinal()).setMaxWidth(30);

        cm.getColumn(LogItem.Field.Time.ordinal()).setWidth(100);
        cm.getColumn(LogItem.Field.Time.ordinal()).setPreferredWidth(100);

        cm.getColumn(LogItem.Field.Level.ordinal()).setPreferredWidth(100);

        cm.getColumn(LogItem.Field.Tag.ordinal()).setPreferredWidth(100);

        cm.getColumn(LogItem.Field.Message.ordinal()).setMinWidth(500);
        cm.getColumn(LogItem.Field.Message.ordinal()).setWidth(500);
        cm.getColumn(LogItem.Field.Message.ordinal()).setPreferredWidth(500);
        cm.getColumn(LogItem.Field.Message.ordinal()).setMaxWidth(500);

        cm.getColumn(LogItem.Field.Pid.ordinal()).setPreferredWidth(100);
        cm.getColumn(LogItem.Field.Tid.ordinal()).setPreferredWidth(100);
        cm.getColumn(LogItem.Field.Module.ordinal()).setPreferredWidth(150);
        cm.getColumn(LogItem.Field.File.ordinal()).setPreferredWidth(150);
        cm.getColumn(LogItem.Field.Line.ordinal()).setPreferredWidth(100);

    }

    private void createStatusBar() {

        _statusPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        //_statusPanel.setBorder(new CompoundBorder(new LineBorder(Color.DARK_GRAY), new EmptyBorder(4, 4, 4, 4)));
        _statusPanel.setBackground(new Color(240,240,240));
        _statusPanel.setSize(MinWidth, 20);

        _statusLabel = new JLabel("ready");
        _statusPanel.add(_statusLabel);

        _frame.add(_statusPanel, BorderLayout.SOUTH);
    }

    private JFrame _frame;

    // tool bar
    private JToolBar _mainToolbar;




    // main panel contains log panel and find bar
    private JPanel _mainPanel;

    private JPanel _findBar;


    // log table and log content
    private JTable _logTable;
    private JScrollPane _logPanel;
    private JTextArea _logContent;
    private JSplitPane _logSplitPanel;

    // status bar
    private JPanel _statusPanel;
    private JLabel _statusLabel;

}